package com.amap.gunfighting;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.amap.mapapi.core.GeoPoint;
import com.amap.mapapi.core.OverlayItem;
import com.amap.mapapi.map.ItemizedOverlay;
import com.amap.mapapi.map.MapView;
import com.amap.mapapi.map.Projection;

class OverItemT extends ItemizedOverlay<OverlayItem> 
{
	String strUrl = "http://83.251.86.183:8080/ZRTrafficWeb/trafficweb";
	//String strUrl = "http://213.103.200.57:8080/zr/trafficweb";
	URL url = null;
	String[] temp;
	int len = 0;
	
	private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
	private List<GeoPoint> GeoP = new ArrayList<GeoPoint>();
	private Drawable marker;  
	private Context mContext; 
	
	public OverItemT(Drawable marker, Context context)
	{	
		super(boundCenterBottom(marker));  
		this.marker = marker;  
		this.mContext = context;  
		
		try
		{
			url = new URL(strUrl);
			HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("POST");
			urlConn.setUseCaches(false);
			urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			urlConn.setRequestProperty("Charest", "utf-8");
			// to connect to the server side
			urlConn.connect();
			
			DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream());
			dop.writeBytes("point=" + URLEncoder.encode("enemyposition","utf-8"));
			//it is essential that to add "&" to separate two strings
			//dop.writeBytes("&username=" + URLEncoder.encode(Map.username,"utf-8"));
			dop.writeBytes("&username=" + URLEncoder.encode("zrfelix","utf-8"));
			dop.flush();
			dop.close();
			
			//get the feedback from the server whether the user's authority is approved 
			DataInputStream dis = new DataInputStream(urlConn.getInputStream());
			String locPassage = dis.readUTF();
			// to disconnect the server side
			urlConn.disconnect();
			// get an array by splitting the received data stream
			temp = locPassage.split(" ");
			// apart from the title of "Succeed", to get the number of rows from the data stream (column = 3 is fixed)
			len = (temp.length-1)/3;
			if("Succeed".equalsIgnoreCase(temp[0]))
			{
				for(int i = 0; i < len; i++)
				{
					// use two arraylist to and GeoPoint and Geolist to show them on the map 
					GeoP.add(new GeoPoint((int) (Double.parseDouble(temp[i*3+1]) * 1E6), (int) (Double.parseDouble(temp[i*3+2]) * 1E6)));
					GeoList.add(new OverlayItem(GeoP.get(i), "", temp[i*3+3]));  
				}
				populate();
			}
		}
		catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	@Override  
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{  
		Projection projection = mapView.getProjection();   
		for (int index = size() -1; index >= 0; index--)
		{
			OverlayItem overLayItem = getItem(index);
			String title= overLayItem.getTitle();   
			Point point = projection.toPixels(overLayItem.getPoint(), null);    
			Paint paintText = new Paint();  
			paintText.setColor(Color.BLACK);  
			paintText.setTextSize(15);  
			canvas.drawText(title, point.x-30, point.y -25, paintText); 
		}  
		super.draw(canvas, mapView, shadow);   
		boundCenterBottom(marker);  
	 }
	
	 @Override  
	protected OverlayItem createItem(int i)
	{  
		 // TODO Auto-generated method stub  
		return GeoList.get(i);  
	}
	
	@Override  
	public int size()
	{  
		// TODO Auto-generated method stub  
		return GeoList.size();  
	}
	
	@Override   
	protected boolean onTap(int i)
	{  
		setFocus(GeoList.get(i));  
		Toast.makeText(this.mContext, GeoList.get(i).getSnippet(),  
		Toast.LENGTH_SHORT).show();  
		return true;  
	}  
	@Override  
	public boolean onTap(GeoPoint point, MapView mapView) {  
	// TODO Auto-generated method stub  
	return super.onTap(point, mapView);  
	 }  
}  